package com.example.text0926;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    MediaPlayer mediaPlayer;
    Context context;
    public MyService() {
        Log.d("mml","My Service" );
//        context = this; 若getApplicationContext()无法获得，则可以使用context
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer = MediaPlayer.create(this,R.raw.music1);
        mediaPlayer.start();
        Log.d("mml","My Service is Start" );
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        Log.d("mml","My Service is onBind" );
        MyBinder mybinder = new MyBinder();
        //IBinder类限制了return的类型为binder
        return mybinder;
    }
    @Override
    public void onDestroy() {
        mediaPlayer.stop();
        Log.d("mml","My Service is Stop" );
        super.onDestroy();
    }

    public class MyBinder extends Binder{
        public MyBinder() {
        }

        public void todo(){
            Log.d("mml","My service My Binder.to do" );
            mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.music1);
            mediaPlayer.start();

        }
    }

}